package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* VolumeAdjustView
* */
class VolumeAdjustView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val startAngle = 310F
    private val sweepAngle = 100F

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_1)

    private var playVoice = true

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            strokeWidth = mStrokeWidth
            color = context.resources.getColor(R.color.color_39E3B8)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            //val cy = mHeight.toFloat() / 2

            val space = mHeight.toFloat() / 30

            if (playVoice) {
                drawArc(
                    space,
                    space,
                    mWidth.toFloat() - space,
                    mHeight.toFloat() - space,
                    startAngle,
                    sweepAngle,
                    false,
                    mPaint0
                )

                drawArc(
                    space * 5,
                    space * 5,
                    mWidth.toFloat() - space * 5,
                    mHeight.toFloat() - space * 5,
                    startAngle,
                    sweepAngle,
                    false,
                    mPaint0
                )

                drawArc(
                    space * 9,
                    space * 9,
                    mWidth.toFloat() - space * 9,
                    mHeight.toFloat() - space * 9,
                    startAngle,
                    sweepAngle,
                    false,
                    mPaint0
                )
            } else {
                mPath0.apply {
                    reset()
                    moveTo(cx + space * 3, space * 9)
                    lineTo(mWidth - space, mHeight - space * 9)
                    moveTo(mWidth - space, space * 9)
                    lineTo(cx + space * 3,mHeight - space * 9)
                }
                drawPath(mPath0, mPaint0)
            }

            // a - space * 4 = mHeight - space * 4 - (mHeight - space * 10)
            mPath0.apply {
                reset()
                moveTo(cx, space * 4)
                lineTo(cx,mHeight - space * 4)
                lineTo(cx * 2 / 5 + space, mHeight - space * 9)
                lineTo(space, mHeight - space * 8)
                lineTo(space, (mHeight - space * 16) / 2)
                lineTo(cx * 2 / 5 + space,space * 9)
                close()
            }
            drawPath(mPath0, mPaint0)
        }
    }

    fun updateFlag(flag: Boolean) {
        this.playVoice = flag
        invalidate()
    }
}